package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class ListAddDelEx extends Activity {
	ArrayList<String> Items;
	ArrayAdapter<String> Adapter;
	ListView list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listadddeltest);
		
		Items = new ArrayList<String>();
		Items.add("신민아");
		Items.add("송지효");
		Items.add("한채아");
		
		Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Items);
		//Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, Items);
		
		list = (ListView) findViewById(R.id.listadddellist);
		list.setAdapter(Adapter);
		list.setOnItemClickListener(mClickListener);
		//list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
	AdapterView.OnItemClickListener mClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			String str = Items.get(position).toString();
			Toast.makeText(ListAddDelEx.this, "현재 값은 : " + str, Toast.LENGTH_SHORT).show();
		}
		
	};	
	
	public void mOnClick(View v){
		EditText et = (EditText) findViewById(R.id.listadddelet);
		switch(v.getId()){
		case R.id.listadddelbtn1:
			String text = et.getText().toString();
			if(text.length() != 0){
				Items.add(text);
				et.setText("");
				Adapter.notifyDataSetChanged();
			}
			break;
		
		case R.id.listadddelbtn2:
			int pos;
			pos = list.getCheckedItemPosition();
			if(pos != ListView.INVALID_POSITION){
				Items.remove(pos);
				list.clearChoices();
				Adapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
